#include"Udpserver.hpp"
#include"comm.hpp"
#include<memory>
#include<vector>
#include"string"
#include<stdio.h>

std::vector<std::string> ban_word={
  "rm",
  "vim",
  "dd",
  "kill",
  "unlick",
  "man",
  "top",
  "exit",
  "vi",
  "chmod"
};



bool SafeChack(std::string& command)
{
  for(auto &k:ban_word)
  {
    std::size_t pos=command.find(k);
    if(pos!=std::string::npos)
      return false;
    
    return true;
  }
}


//using cb_t =std::function<std::string(std::string)>;
std::string OnMessage(std::string request)
{
  return request+="[message got you!!]";
}

void Usage(std::string proc)
{
  std::cout<<"Usage: \n\t"<<proc<<"local_prot"<<std::endl;
}

std::string ExecuteCommand(std::string command)
{
  if(!SafeChack(command)) return "bad!!";

  FILE* fp=popen(command.c_str(),"r");
  
  if(fp==nullptr)
    return "excute fail,reason know";

  std::string respond;
  char buffer[1024];
  while(true)
  {
    char*s=fgets(buffer,sizeof(buffer),fp);
    if(!s) break;
    else
      respond +=buffer;
  }
  return respond;

}


int main(int args,char*argv[])
{
  if(args!=2)
  {
    Usage(argv[0]);
    return Usage_err;
  }

  //std::string ip=argv[1];
  uint16_t port=std::stoi(argv[1]);
  //std::unique_ptr<UdpServer> usvr = std::unique_ptr<UdpServer>(new UdpServer(port));
  //std::unique_ptr<UdpServer> usvr = std::unique_ptr<UdpServer>(new UdpServer(ExecuteCommand,port));
  std::unique_ptr<UdpServer> usvr = std::unique_ptr<UdpServer>(new UdpServer(port));
  usvr->Init();
  usvr->Start();
  
  return 0;
}


